package com.adamfass.tractorbeam.gameobject;

import com.adamfass.tractorbeam.Blocker;
import com.jme.bounding.OrientedBoundingBox;
import com.jme.math.Matrix3f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;

public class DisruptorField implements Blocker {
  private final Box box;
  
  public DisruptorField(Node rootNode, Vector3f position, Vector3f normal) {
    this.box = new Box("disruptor field", new Vector3f(), 75, 5, 75);
    rootNode.attachChild(box);
    
    final MaterialState materialState =
      DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
    materialState.setEnabled(true);
    materialState.setDiffuse(ColorRGBA.red);
    materialState.setAmbient(ColorRGBA.red);
    box.setRenderState(materialState);
    box.updateRenderState();
    
    box.setLocalTranslation(position);
    final Matrix3f rotation = new Matrix3f();
    rotation.fromStartEndVectors(Vector3f.UNIT_Y, normal);
    box.setLocalRotation(rotation);
    box.updateGeometricState(0, true);
    
    box.setModelBound(new OrientedBoundingBox());
    box.updateModelBound();
  }
  
  public Spatial getBlockerSpatial() {
    return box;
  }
  
  public void cleanUp() {
    box.removeFromParent();
  }
}
